<?php 
	class Template{
		function __construct($templates_dir = "./templates/", $complie_dir = "./templates_c/"){
			$this->templates_dir = $templates_dir;
			$this->complie_dir = $complie_dir;
			$this->tpl_vars = array();
		}
		
		
		function assign($tpl_var, $value = "null"){
			if($tpl_var != ''){
				$this->tpl_vars[$tpl_var] = $value;
			}
		}
	
		function display($fileName){
			$tplFile = $this->templates_dir.$fileName;
			if(!file_exists($tplFile)){
				echo "tpFile False!!!";
				exit();
			}
			$complieTemplateFile = $this->complie_dir."complie_".basename($tplFile).".php";
			
			if(!file_exists($complieTemplateFile) || filemtime($complieTemplateFile) < filemtime($tplFile)){
				$repContent = $this->tpl_replace(file_get_contents($tplFile));
				$handle = fopen($complieTemplateFile, 'w+');
				fwrite($handle, $repContent);
				fclose($handle);
			}
			
			include $complieTemplateFile;
		}
		
		private function tpl_replace($content){
			$pattern = array(
			'/<\{\s*\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*\}\>/i',
			'/<\{\s*if\s*(.+?)\s*\}>(.+?)<\{\s*\/if\s*\}>/ies',
			'/<\{\s*else\s*if\s*(.+?)\s*\}>/ies',
			'/<\{\s*else\s*\}>/is',
			'/<\{\s*loop\s+\$(\S+)\s+\$([a-zA-Z_x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*\}>(.+?)<\{\s*\/loop\s*\}>/is',
			'/<\{\s*loop\s+\$(\S+)\s+\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*=>\s*\$(\S+)\s*\}>(.+?)<\{\s*\/loop\s*\}>/is',
			'/<\{\s*include\s+[\"\']?(.+?)[\"\']?\s*\}>/ie',			
			);
			
			$replacement = array(
			'<?php echo $this->tpl_vars["${1}"];?>',
		    '$this->stripvtags(\'<?php if(${1}){?>\',\'${2}<?php } ?>\')',
			'$this->stripvtags(\'<?php } elseif(${1}){ ?>\',"")',
			'<?php}else{?>', 
			'<?php foreach($this->tpl_vars["${1}"] as $this->tpl_vars["${2}"]) {?> ${3} <?php }?>',
			'<?php foreach($this->tpl_vars["${1}"] as $this->tpl_vars["${2}"] => $this->tpl_vars["${3}"]) {?> ${4} <?php }>',
			'file_get_contents($this->templates_dir."${1}")', 		
			);
			
			$repContent = preg_replace($pattern, $replacement, $content);
			if(preg_match('/<\{([^(\}>)]{1,})\}>/',$repContent)){
				$repContent = $this->tpl_replace($repContent);
			}
			
			return $repContent;	
		
		}
		
		private function stripvtags($expr, $statement=''){
			$var_patten = '/\s*\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*/is';
			$expr = preg_replace($var_patten,'$this->tpl_vars["${1}"]',$expr);
			$expr = str_replace("\\\"","\"", $expr);
			$statement = str_replace("\\\"","\"", $statement);
			return $expr.$statement;
		}
	
	}




